package com.douxg.learning.springboot.redisTemplate;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Description
 * @Auther douxg
 * @Date 2021/12/20 11:19
 */
@RestController
@RequestMapping("/redis")
public class Main {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/get")
    public String get(@RequestParam String key) {
        return Optional.ofNullable(stringRedisTemplate.opsForValue().get(key)).map(o -> o.toString()).orElse(null);
    }

    @GetMapping("/set")
    public String set(@RequestParam String key, @RequestParam String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return "true";
    }

}
